// subPages/business/newGoods/newGoods.js
const app = getApp()
var util =  require('../../../utils/util.js');
var index = 1,picType = ''
var flag = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array:[0],//默认显示一个
    inputName:[],//所有input的内容
    inputVal:[],//所有input的内容
    isHaveParamete: false,
    isOne: true,
    btnTxt: '',
    thumbnailList: [{ tempFilePaths: [], imgList: [] }],//缩略图
    bannerList: [{ tempFilePaths: [], imgList: [] }],//轮播图
    detailsList: [{ tempFilePaths: [], imgList: [] }],//详情图
    downImg: '/resources/business/down2.png',
    upImg: '/resources/business/up2.png',
    statusList: [
      { name: '1', value: '午餐', checked: false},
      { name: '2', value: '晚餐', checked: false}
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    if(options.type){
      that.setData({
        shopType: app.globalData.shopType,
        type: options.type,
        categoryImgSrc: that.data.downImg,
        dateImgSrc: that.data.downImg
      })
    }
    var title = ''
    switch (that.data.shopType) {
      case '0':
        that.setData({
          btnTxt: '完成',
        })
        if(that.data.type == '1'){
          title = '添加商品'
          //获取商品分类
          that.getClass()
        }else if(that.data.type == '2'){
          if(options.item){
            title = '修改商品'
            var goodsInfo = JSON.parse(options.item)
            that.setData({
              goodsInfo: goodsInfo
            })
            that.setServiceGoodsInfo(that.data.goodsInfo)
          }
        }
        break;
      case '1':
        that.setData({
          btnTxt: '下一步'
        })
        if(that.data.type == '1'){
          title = '添加商品'
          //获取商品分类
          that.getClass()
        }else if(that.data.type == '2'){
          if(options.item){
            title = '修改商品'
            var goodsInfo = JSON.parse(options.item)
            that.setData({
              goodsInfo: goodsInfo
            })
            that.setGoodsInfo(that.data.goodsInfo)
          }
        }
        break;
      case '2':
        that.setData({
          btnTxt: '完成',
          mainType: options.mainType,
        })
        if(that.data.type == '1'){
          title = '上传菜品'
          //获取商品分类
          that.getClass()
        }else if(that.data.type == '2'){
          if(options.id){
            title = '修改菜品'
            that.setData({
              kitchenGoodsid: options.id
            })
            that.getKitchenInfo(options.id)
          }
        }
        break;
      case '3':
        if(options.mainItemType){
          that.setData({
            btnTxt: '完成',
            mainItemType: options.mainItemType
          })
          if(that.data.mainItemType == 1){
            if(that.data.type == '1'){
              title = '添加物业公告'
            }else if(that.data.type == '2'){
              if(options.item){
                title = '修改物业公告'
                var propertyInfo = JSON.parse(options.item)
                that.setData({
                  propertyInfo: propertyInfo
                })
                that.setPropertyInfo(that.data.propertyInfo)
              }
            }
          }else{
            if(that.data.type == '1'){
              title = '添加活动展示'
            }else if(that.data.type == '2'){
              if(options.item){
                title = '修改活动展示'
                var activitiyInfo = JSON.parse(options.item)
                that.setData({
                  activitiyInfo: activitiyInfo
                })
                that.setactivitiyInfo(that.data.activitiyInfo)
              }
            }
          }
        }
        break;
    }
    
    util.setNavBarTitle(title)
  },

  //设置数据
  setGoodsInfo:function(goodsInfo){
    if(goodsInfo){
      var that = this
      // console.log(goodsInfo)
      //缩略图
      let thumbnailImg = goodsInfo.head_img.substring(0,5) == 'http:'?goodsInfo.head_img.replace('http:','https:'):goodsInfo.head_img;
      var thumbnailImgList = []
      thumbnailImgList.push(thumbnailImg)
      //banner图
      let bannerImgs = goodsInfo.top_img
      for(let i in bannerImgs){
        bannerImgs[i].banner_img = bannerImgs[i].banner_img.substring(0,5) == 'http:'?bannerImgs[i].banner_img.replace('http:','https:'):bannerImgs[i].banner_img;
      }
      //详情图
      let detailsImgs = goodsInfo.content_img
      for(let j in detailsImgs){
        detailsImgs[j].url = detailsImgs[j].url.substring(0,5) == 'http:'?detailsImgs[j].url.replace('http:','https:'):detailsImgs[j].url;
      }

      that.setData({
        name: goodsInfo.product_name,
        price: goodsInfo.price,
        shopNumber: goodsInfo.pay_no,
        specification: goodsInfo.gg,
        introduce: goodsInfo.content,
        thumbnailList: [{ tempFilePaths: [], imgList: thumbnailImgList }],//缩略图
        bannerList: [{ tempFilePaths: [], imgList: bannerImgs }],//轮播图
        detailsList: [{ tempFilePaths: [], imgList: detailsImgs }],//详情图
      })

      util.getAjax(app.globalData.apiUrl +'/get_community_health_shop_product_class',function(res){
        console.log(res)
        if(res.data.length!=0){
          var categoryArray = []
          let categoryIndex = ''
          for(var i in res.data){
            categoryArray.push(res.data[i])
            if(res.data[i].id == goodsInfo.class_id){
              categoryIndex = i
            }
          }
          that.setData({
            isCategory: true,
            categoryArray: categoryArray,
            categoryIndex: categoryIndex,
            class_name: categoryArray[categoryIndex].class_name,
          })
        }
      },{community_id:  app.globalData.communityContent.community_id})

      //参数
      var newArray = that.data.array;
      for(let c in goodsInfo.canshu){
        if(goodsInfo.canshu.length == 1){
          index = 1
          let oldName = that.data.inputName;
          oldName[c] =  goodsInfo.canshu[c].name;//修改对应索引值的内容
          let oldVal = that.data.inputVal;
          oldVal[c]= goodsInfo.canshu[c].text;//修改对应索引值的内容
          that.setData({
            inputName: oldName,
            inputVal: oldVal,
          })
        }else{
          if(c == 0){
            index = 1
            let oldName = that.data.inputName;
            oldName[c] =  goodsInfo.canshu[c].name;//修改对应索引值的内容
            let oldVal = that.data.inputVal;
            oldVal[c]= goodsInfo.canshu[c].text;//修改对应索引值的内容
            that.setData({
              inputName: oldName,
              inputVal: oldVal,
            })
          }else{
            newArray.push(1);
            index ++
            let oldName = that.data.inputName;
            oldName[c] =  goodsInfo.canshu[c].name;//修改对应索引值的内容
            let oldVal = that.data.inputVal;
            oldVal[c]= goodsInfo.canshu[c].text;//修改对应索引值的内容
            that.setData({
              isHaveParamete: true,
              array: newArray,
              inputName: oldName,
              inputVal: oldVal,
            })
          }
        }
      }
    }
  },

  //设置社区商城商品数据
  setServiceGoodsInfo:function(goodsInfo){
    if(goodsInfo){
      var that = this
      console.log(goodsInfo)
      //缩略图
      var thumbnailImgList = []
      if(goodsInfo.head_img){
        let thumbnailImg = goodsInfo.head_img.substring(0,5) == 'http:'?goodsInfo.head_img.replace('http:','https:'):goodsInfo.head_img;
        thumbnailImgList.push(thumbnailImg)
      }
      that.setData({
        name: goodsInfo.product_name,
        oldPrice: goodsInfo.no_price,
        newPrice: goodsInfo.price,
        shopNumber: goodsInfo.no_pay,
        specification: goodsInfo.gg,
        thumbnailList: [{ tempFilePaths: [], imgList: thumbnailImgList }],//缩略图
      })

      util.getAjax(app.globalData.apiUrl +'/get_community_service_shop_product_class',function(res){
        console.log(res)
        if(res.data.length!=0){
          var categoryArray = []
          let categoryIndex = ''
          for(var i in res.data){
            categoryArray.push(res.data[i])
            if(res.data[i].id == goodsInfo.class_id){
              categoryIndex = i
            }
          }
          that.setData({
            isCategory: true,
            categoryArray: categoryArray,
            categoryIndex: categoryIndex,
            class_name: categoryArray[categoryIndex].class_name,
          })
        }
      },{shop_id: app.globalData.shopId})
    }
  },

  //获取特膳厨房数据
  getKitchenInfo:function(id){
    var that = this
    util.getAjax(`${app.globalData.apiUrl}/get_community_service_kitchen_product_content`,function(res){
      console.log(res)
      if(res.data){
        var thumbnailImgList = []
        if(res.data.head_img){
          let thumbnailImg = res.data.head_img.substring(0,5) == 'http:'?res.data.head_img.replace('http:','https:'):res.data.head_img;
          thumbnailImgList.push(thumbnailImg)
        }
        that.setData({
          name: res.data.product_name,
          
          main: res.data.function,
          food: res.data.food,
          price: res.data.price,
          thumbnailList: [{ tempFilePaths: [], imgList: thumbnailImgList }],//缩略图
          
        })

        util.getAjax(app.globalData.apiUrl +'/get_community_service_kitchen_class',function(resc){
          console.log(resc)
          if(resc.data.length!=0){
            var categoryArray = []
            let categoryIndex = ''
            for(var i in resc.data){
              categoryArray.push(resc.data[i])
              if(resc.data[i].id == res.data.class_id){
                categoryIndex = i
              }
            }
            that.setData({
              isCategory: true,
              categoryArray: categoryArray,
              categoryIndex: categoryIndex,
              class_name: categoryArray[categoryIndex].class_name,
            })
          }
        },{kitchen_id: app.globalData.shopId,type: that.data.mainType})
      }
      if(that.data.mainType == '1'){
        that.setData({
          suitable: res.data.suitable,
        })
      }
      if(that.data.mainType == '2'){
        var statusList = that.data.statusList
        for(var i in statusList){
          if(statusList[i].name == res.data.status){
            statusList[i].checked = true
            break;
          }
        }
        that.setData({
          statusList: statusList,
          kitchenStatus: res.data.status,
          isDate: true,
          date: res.data.date,
        })
      }
    },{id: id})
  },

  //设置物业公告数据
  setPropertyInfo:function(propertyInfo){
    if(propertyInfo){
      var that = this
      console.log(propertyInfo)
      that.setData({
        title: propertyInfo.title,
        property: propertyInfo.content
      })
    }
  },

  //设置活动展示
  setactivitiyInfo:function(activitiyInfo){
    if(activitiyInfo){
      // console.log(activitiyInfo)
      var that = this
      //未完 详情图
      var detailsImgs = []
      if(activitiyInfo.img){
        detailsImgs = activitiyInfo.img
        for(let j in detailsImgs){
          detailsImgs[j].url = detailsImgs[j].url.substring(0,5) == 'http:'?detailsImgs[j].url.replace('http:','https:'):detailsImgs[j].url;
        }
      }
      that.setData({
        content: activitiyInfo.content,
        detailsList: [{ tempFilePaths: [], imgList: detailsImgs }],//详情图
      })
    }
  },

  //picker
  bindCategoryChange: function (e) {
    if(this.data.categoryArray){
      this.setData({
        categoryIndex: e.detail.value,
        category: this.data.categoryArray[e.detail.value],
        isCategory: true,
        categoryImgSrc: this.data.downImg
      })
    }
  },

  bindCategoryCancel:function(){
    this.setData({
      categoryImgSrc: this.data.downImg
    })
  },

  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value,
      dateImgSrc: this.data.downImg,
      isDate: true
    })
  },

  bindDateCancel:function(){
    this.setData({
      dateImgSrc: this.data.downImg
    })
  },

  radioChange: function (e) {
    this.setData({
      kitchenStatus: e.detail.value
    })
    console.log(e.detail.value)
  },

  //选择箭头
  imgChange:function(e){
    var that = this
    switch (e.currentTarget.dataset.type) {
      case '分类':
        if(that.data.categoryImgSrc == that.data.downImg){
          that.setData({
            categoryImgSrc: that.data.upImg
          })
        }
        break;
      case '日期':
        if(that.data.dateImgSrc == that.data.downImg)
        {
          that.setData({
            dateImgSrc: that.data.upImg
          })
        }
        break;
    }
  },

  //获取分类
  getClass:function(){
    var that = this
    var urlType = ''
    var data = {}
    switch (that.data.shopType) {
      case '0':
        urlType = 'get_community_service_shop_product_class'
        data = {shop_id: app.globalData.shopId}
        break;
      case '1':
        urlType = 'get_community_health_shop_product_class'
        data = {community_id:  app.globalData.communityContent.community_id}
        break;
      case '2':
        urlType = 'get_community_service_kitchen_class'
        data = {kitchen_id: app.globalData.shopId,type: that.data.mainType}
        break;
    }
    util.getAjax(`${app.globalData.apiUrl}/${urlType}`,that.getGoodsClass,data)
  },

  //获取分类
  //获取商品分类
  getGoodsClass(res){
    console.log(res)
    var that = this
    if(res.data.length != 0){
      var categoryArray = []
      for(var i in res.data){
        categoryArray.push(res.data[i])
      }
      that.setData({
        categoryArray: categoryArray,
      })
    }
  },

  //添加参数
  addParamete:function(){
    var that = this
    var newArray = that.data.array;
    newArray.push(1);//这里不管push什么，只要数组长度增加1就行
    that.setData({
      array: newArray,
      isHaveParamete: true
    })
    index ++
  },

  //删除参数
  delParamete:function(e){
    var oldInputName = this.data.inputName;
    var oldInputVal = this.data.inputVal;//所有的input值
    var oldarr = this.data.array;//循环内容
    oldarr.splice(index-1,1);    //删除当前索引的内容，这样就能删除view了
    oldInputName.splice(index-1,1);//view删除了对应的input值也要删掉
    oldInputVal.splice(index-1,1);//view删除了对应的input值也要删掉
    index --
    if (oldarr.length < 1) {
      oldarr = [0]  //如果循环内容长度为0即删完了，必须要留一个默认的。这里oldarr只要是数组并且长度为1，里面的值随便是什么
      this.setData({
        isHaveParamete: false
      })
      index = 1
    }
    this.setData({
      array:oldarr,
      inputName: oldInputName,
      inputVal: oldInputVal
    })
  },

  getInputName:function(e){
    var nowIdx = e.currentTarget.dataset.idx;//获取当前索引
    var val = e.detail.value;//获取输入的值
    var oldName = this.data.inputName;
    oldName[nowIdx]= val;//修改对应索引值的内容
    this.setData({
      inputName:oldName
    })
  },

  //获取input的值
  getInputVal:function(e){
    var nowIdx = e.currentTarget.dataset.idx;//获取当前索引
    var val = e.detail.value;//获取输入的值
    var oldVal = this.data.inputVal;
    oldVal[nowIdx]= val;//修改对应索引值的内容
    this.setData({
      inputVal:oldVal
    })
  },

  //获取list
  getPicList:function(){
    var that = this
    var list = []
    switch (picType) {
      case 'thumbnail':
        list = that.data.thumbnailList
        break;
      case 'banner':
        list = that.data.bannerList
        break;
      case 'details':
        list = that.data.detailsList
        break;
    }
    return list
  },

  //添加图片
  joinPicture: function (e) {
    var that = this;
    picType = e.currentTarget.dataset.type;
    var index = e.currentTarget.dataset.index;
    var list = that.getPicList();
    // console.log(list)
    var imgNumber = list[index].tempFilePaths;
    let count = 0
    switch (picType) {
      case 'thumbnail':
        count = 1
        break;
      case 'banner':
      case 'details':
        count = 15
        break;
    }
    if (imgNumber.length >= count) {
      util.showModal('提示','最多上传'+ count +'张图片')
      return;
    }
    wx.showActionSheet({
      itemList: ["从相册中选择", "拍照"],
      itemColor: "#f7982a",
      success: function (res) {
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            that.chooseWxImage("album", imgNumber);
          } else if (res.tapIndex == 1) {
            that.chooseWxImage("camera", imgNumber);
          }
        }
      }
    })
  },

  //选择图片
  chooseWxImage: function (type, list) {
    var img = list;
    var len = img.length;
    var that = this;
    var list = that.getPicList();
    // var evalList = this.data.evalList;
    let count = 0
    switch (picType) {
      case 'thumbnail':
        count = 1
        break;
      case 'banner':
      case 'details':
        count = 15
        break;
    }
    wx.chooseImage({
      count: count,
      sizeType: ["original", "compressed"],
      sourceType: [type],
      success: function (res) {
        // console.log(res)
        var addImg = res.tempFilePaths;
        var addLen = addImg.length;
        if ((len + addLen) > count) {
          for (var i = 0; i < (addLen - len); i++) {
            var str = {};
            str.pic = addImg[i];
            img.push(str);
          }
        } else {
          for (var j = 0; j < addLen; j++) {
            var str = {};
            str.pic = addImg[j];
            img.push(str);
          }
        }
        switch (picType) {
          case 'thumbnail':
            that.setData({
              thumbnailList: list
            })
            break;
          case 'banner':
            that.setData({
              bannerList: list
            })
            break;
          case 'details':
            that.setData({
              detailsList: list
            })
            break;
        }
        // that.setData({
        //   evalList: evalList
        // })
        that.upload(img);
      },
    })
  },

  //删除图片
  clearImg: function (e) {
    var that = this
    var index = e.currentTarget.dataset.index;
    picType = e.currentTarget.dataset.type;
    var list = that.getPicList();
    // var evalList = that.data.evalList;
    wx.request({
      url: app.globalData.apiUrl +'/Api/First/delete',
      data: {
        file: list[0].tempFilePaths[index]
      },
      method: 'get',
      header: {
        'content-type': 'application/json' //默认值
      },
      success: function (res) {
        // console.log(res)
        if(res.data == 1){
          var img = list[0].tempFilePaths;
          var img2 = list[0].imgList;
          img.splice(index, 1);
          img2.splice(index, 1);        
          switch (picType) {
            case 'thumbnail':
              that.setData({
                thumbnailList: list
              })
              break;
            case 'banner':
              that.setData({
                bannerList: list
              })
              break;
            case 'details':
              that.setData({
                detailsList: list
              })
              break;
          }  
          // that.setData({
          //   evalList: evalList
          // })
        }
      },
      fail: function (res) {
        console.log("失败",res);
      }
    })
  },

  //多张图片上传
  upload: function (path) {
    var that = this;
    var curImgList = [];
    for (var i = 0; i < path.length; i++) {
      wx.showToast({
        icon: "loading",
        title: "正在上传"
      }),
      wx.uploadFile({
        url: app.globalData.apiUrl + '/upload',//接口处理在下面有写
        filePath: path[i].pic,
        name: 'img',
        header: { "Content-Type": "multipart/form-data" },
        formData: {
          douploadpic: '1'
        },
        success: function (res) {
          // console.log(res)
          let data = {}
          switch (picType) {
            case 'thumbnail':
              curImgList.push('https://jzj.freei.cc'+res.data);
              break;
            case 'banner':
              data = {
                banner_img: 'https://jzj.freei.cc'+res.data
              }
              curImgList.push(data);
              break;
            case 'details':
              data = {
                url : 'https://jzj.freei.cc'+res.data
              }
              curImgList.push(data);
              break;
          }
          // var evalList = that.data.evalList;
          var list = that.getPicList();
          list[0].imgList = curImgList;
          console.log(curImgList)
          switch (picType) {
            case 'thumbnail':
              that.setData({
                thumbnailList: list
              })
              break;
            case 'banner':
              that.setData({
                bannerList: list
              })
              break;
            case 'details':
              that.setData({
                detailsList: list
              })
              break;
          }
          // that.setData({
          //   evalList: evalList
          // })
        },
        fail: function (e) {
          util.showModal('提示','上传失败')
          return
        },
        complete: function () {
          wx.hideToast();  //隐藏Toast
        }
      })
    }
  },

  //设置缩略图片
  setThumbnailImg:function(){
    var that = this 
    var thumbnailList = that.data.thumbnailList
    var imgString = ''
    if(thumbnailList){
      var imgList = thumbnailList[0].imgList
      for (var j = 0; j < imgList.length; j++) {
        imgString = imgList[j]
      }
      return imgString
    }
  },

  //设置轮播图片
  setBannerImg:function(){
    var that = this 
    var bannerList = that.data.bannerList
    var imgPort = [];//图片地址，多张以逗号分割
    if(bannerList){
      var imgList = bannerList[0].imgList
      for (var j = 0; j < imgList.length; j++) {
        imgPort.push(imgList[j])
      }
    }
    // console.log(imgPort)
    return imgPort
  },

  //设置详情图片
  setDetailsImg:function(){
    var that = this 
    var detailsList = that.data.detailsList
    var imgPort = [];//图片地址，多张以逗号分割
    if(detailsList){
      var imgList = detailsList[0].imgList
      for (var j = 0; j < imgList.length; j++) {
        imgPort.push(imgList[j])
      }
    }
    // console.log(imgPort)
    return imgPort
  },

  formSubmit:function(e){
    // console.log(e)
    if(flag){
      return
    }
    flag = true
    var that = this
    var newDataJson = []
    if(that.data.shopType == '0'){
      var name = e.detail.value.name
      var oldPrice = e.detail.value.oldPrice
      var newPrice = e.detail.value.newPrice      
      var shopNumber = e.detail.value.shopNumber
      var specification = e.detail.value.specification
      var thumbnailImg = that.setThumbnailImg()
      if(!that.data.isCategory){
        util.showModal('提示','请选择分类')
        flag = false
        return
      }
      if(name == '' ||oldPrice == '' || newPrice == '' || specification == '' || shopNumber == '' || thumbnailImg.length == 0){
        util.showModal('提示','请填写完整信息')
        flag = false
        return 
      }
      newDataJson.push(name,oldPrice,newPrice,shopNumber,specification,thumbnailImg)
      that.save(newDataJson)
    }else if(that.data.shopType == '1'){
      var parameterList = []
      if(that.data.inputName.length != 0 &&that.data.inputVal.length != 0){
        for(var i=0;i < index;i++){
            if(!that.data.inputName[i]||!that.data.inputVal[i]){
              util.showModal('提示','请输入参数')
              flag = false
              return
            }
            var data = {
              name: that.data.inputName[i],
              text: that.data.inputVal[i]
            }
            parameterList.push(data)
        }
      }else{
        util.showModal('提示','请输入参数')
        flag = false
        return
      }
      if(!that.data.isCategory){
        util.showModal('提示','请选择分类')
        flag = false
        return
      }
      if(that.data.isOne){
        var name = e.detail.value.name
        var price = e.detail.value.price
        var shopNumber = e.detail.value.shopNumber
        var specification = e.detail.value.specification
        var introduce = e.detail.value.introduce
        if(name == '' ||price == '' || specification == '' || introduce == '' || shopNumber == ''){
          util.showModal('提示','请填写完整信息')
          flag = false
          return 
        }
        that.setData({
          name: name,
          price: price,
          shopNumber: shopNumber,
          specification: specification,
          introduce: introduce,
          isOne: false,
          btnTxt: '完成'
        })
      }else{
        var thumbnailImg = that.setThumbnailImg()
        var bannerImg = that.data.bannerList[0].imgList
        var detailsImg = that.data.detailsList[0].imgList
        if(thumbnailImg.length == 0 || bannerImg.length == 0 || detailsImg.length == 0){
          util.showModal('提示','请上传完整信息')
          flag = false
          return
        }
        newDataJson.push(that.data.name,that.data.price,that.data.shopNumber,that.data.specification,that.data.introduce,parameterList,thumbnailImg,bannerImg,detailsImg)
        that.save(newDataJson)
      }
    }else if(that.data.shopType == '2'){
      var name = e.detail.value.name
      var main = e.detail.value.main      
      var food = e.detail.value.food
      var price = e.detail.value.price
      var thumbnailImg = that.setThumbnailImg()
      if(!that.data.isCategory){
        util.showModal('提示','请选择分类')
        flag = false
        return
      }
      if(that.data.mainType == '2'){
        if(!that.data.isDate){
          util.showModal('提示','请选择日期')
          flag = false
          return
        }
        if(!that.data.kitchenStatus){
          util.showModal('提示','请选择时间节点')
          flag = false
          return
        }
      }
      if(that.data.mainType == 1){
        var suitable = e.detail.value.suitable
        if(suitable == ''){
          util.showModal('提示','请填写适应人群')
          flag = false
          return
        }else{
          that.setData({
            suitable: suitable
          })
        }
      }
      if(name == '' || main == '' || food == '' || price == '' || thumbnailImg == []){
        util.showModal('提示','请填写完整信息')
        flag = false
        return 
      }
      newDataJson.push(name,main,food,price,thumbnailImg)
      that.save(newDataJson)
    }else if(that.data.shopType == '3'){
      if(that.data.mainItemType == 1){
        var title = e.detail.value.title
        var property = e.detail.value.property      
        if(title == '' || property == ''){
          util.showModal('提示','请填写完整信息')
          flag = false
          return 
        }
        newDataJson.push(title,property)
        that.save(newDataJson)
      }else{
        var content = e.detail.value.content
        var detailsImg = that.data.detailsList[0].imgList
        if(content == ''){
          util.showModal('提示','请填写完整信息')
          flag = false
          return 
        }
        newDataJson.push(content,detailsImg)
        that.save(newDataJson)
      }
    }
  },

  //提交
  save:function(newDataJson){
    var that = this
    var data = {}
    var urlType = ''
    if(that.data.shopType == '0'){
      urlType = 'admin_operation_community_service_shop_product'
      if(that.data.type == '1'){
        data = {
          "shop_id": app.globalData.shopId,
          "product_name": newDataJson[0],
          "no_price":newDataJson[1],
          "price": newDataJson[2],
          "no_pay": newDataJson[3],
          "gg": newDataJson[4],
          "head_img": newDataJson[5],
          "class_id": that.data.categoryArray[that.data.categoryIndex].id,
        }
      }else if(that.data.type == '2'){
        data = {
          "id": that.data.goodsInfo.id,
          "shop_id": app.globalData.shopId,
          "product_name": newDataJson[0],
          "no_price":newDataJson[1],
          "price": newDataJson[2],
          "no_pay": newDataJson[3],
          "gg": newDataJson[4],
          "head_img": newDataJson[5],
          "class_id": that.data.categoryArray[that.data.categoryIndex].id,
        }
      }
    }else if(that.data.shopType == '1'){
      urlType = 'admin_operation_community_health_shop_product'
      if(that.data.type == '1'){
        data = {
          "shop_id": app.globalData.shopId,
          "product_name": newDataJson[0],
          "price":newDataJson[1],
          "pay_no": newDataJson[2],
          "gg": newDataJson[3],
          "content": newDataJson[4],     
          "canshu": newDataJson[5],     
          "head_img": newDataJson[6],
          "top_img": newDataJson[7],          
          "content_img": newDataJson[8],
          "status": 0,
          "class_id": that.data.categoryArray[that.data.categoryIndex].id,
          'yunfei': 0,
          "city": app.globalData.city,
          "community_id": app.globalData.communityContent.community_id,
        }
      }else if(that.data.type == '2'){
        data = {
          "id": that.data.goodsInfo.id,
          "shop_id": app.globalData.shopId,
          "product_name": newDataJson[0],
          "price":newDataJson[1],
          "pay_no": newDataJson[2],
          "gg": newDataJson[3],
          "content": newDataJson[4],     
          "canshu": newDataJson[5],     
          "head_img": newDataJson[6],
          "top_img": newDataJson[7],          
          "content_img": newDataJson[8],
          "status": 0,
          "class_id": that.data.categoryArray[that.data.categoryIndex].id,
          'yunfei': 0,
          "city": app.globalData.city,
          "community_id": app.globalData.communityContent.community_id,
        }
      }
    }else if(that.data.shopType == '2'){
      urlType = 'operation_community_service_kitchen_product'
      if(that.data.type == '1'){
        data = {
          "kitchen_id": app.globalData.shopId,
          "product_name": newDataJson[0],
          "function": newDataJson[1],
          "food": newDataJson[2],
          "price": newDataJson[3],
          "head_img": newDataJson[4],
          "class_id": that.data.categoryArray[that.data.categoryIndex].id,
          "suitable": that.data.mainType == '1'?that.data.suitable:null,
          "date": that.data.mainType == '2'?that.data.date:null,
          "status": that.data.mainType == '2'?that.data.kitchenStatus:null
        }
      }else if(that.data.type == '2'){
        data = {
          "id": that.data.kitchenGoodsid,
          "kitchen_id": app.globalData.shopId,
          "product_name": newDataJson[0],
          "function": newDataJson[1],
          "food": newDataJson[2],
          "price": newDataJson[3],
          "head_img": newDataJson[4],
          "class_id": that.data.categoryArray[that.data.categoryIndex].id,
          "suitable": that.data.mainType == '1'?that.data.suitable:null,
          "date": that.data.mainType == '2'?that.data.date:null,
          "status": that.data.mainType == '2'?that.data.kitchenStatus:null
        }
      }
    }else if(that.data.shopType == '3'){
      if(that.data.mainItemType=='1'){
        urlType = 'admin_operation_community_member_center_property_notice'
        if(that.data.type == '1'){
          data = {
            "title": newDataJson[0],
            "content": newDataJson[1],
            "date": util.setTime4(new Date()),
            "community_id": app.globalData.shopId
          }
        }else if(that.data.type == '2'){
          data = {
            "id": that.data.propertyInfo.id,
            "title": newDataJson[0],
            "content": newDataJson[1],
            "date": util.setTime4(new Date()),
            "community_id": app.globalData.shopId
          }
        }
      }else{
        urlType = 'operation_community_member_center_hd_news'
        if(that.data.type == '1'){
          data = {
            "content": newDataJson[0],
            "img": newDataJson[1],
            "date": util.formatTime(new Date()),
            "community_id": app.globalData.shopId,
            "user_id": app.globalData.shopUserId
          }
        }else if(that.data.type == '2'){
          data = {
            "id": that.data.activitiyInfo.id,
            "content": newDataJson[0],
            "img": newDataJson[1],
            "date": util.formatTime(new Date()),
            "community_id": app.globalData.shopId,
            "user_id": app.globalData.shopUserId
          }
        }
      }
    }
    data = JSON.stringify(data);
    var text = ''
    if(that.data.shopType == '2'){
      if(that.data.type == '2'){
        text = '菜单信息，修改成功！';
      }else if(that.data.type == '1'){
          text = '新的菜单，添加成功！';
      }
    }else if(that.data.shopType == '3'){
      if(that.data.mainItemType == '1'){
        if(that.data.type == '2'){
          text = '物业公告，修改成功！';
        }else if(that.data.type == '1'){
            text = '新的物业公告，添加成功！';
        }
      }else{
        if(that.data.type == '2'){
          text = '活动展示，修改成功！';
        }else if(that.data.type == '1'){
            text = '新的活动展示，添加成功！';
        }
      }
    }else{
      if(that.data.type == '2'){
        text = '商品信息，修改成功！';
      }else if(that.data.type == '1'){
          text = '新的商品，添加成功！';
      }
    }
    util.postAjax(`${app.globalData.apiUrl}/${urlType}`,function(res){
      // console.log(res)
      if(res.data == 1){
        util.showToast(text,'success',2000)
        setTimeout(function(){
          wx.navigateBack({
            delta: 1, // 回退前 delta(默认为1) 页面
          })
          flag = false
        },500)
      }else{
        util.showModal('提示','提交失败，请重试')
        flag = false
      }
    },{
        type: that.data.type,
        pro: data
    })
  },

  //点击图片
  imgBtn:function(e){
    // console.log(e)
    var list = e.currentTarget.dataset.list;
    var index = e.currentTarget.dataset.index;
    var type = e.currentTarget.dataset.type;
    let arr = []
    for(var i in list){
      switch (type) {
        case 'thumbnail':
          arr.push(list[i])
          break;
        case 'banner':
          arr.push(list[i].banner_img)
          break;
        case 'details':
          arr.push(list[i].url)
          break;
      }
    }
    wx.previewImage({
      urls: arr,
      current: arr[index]
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})